/**
 * 
 */
package com.ffcs.crmd.tsp.server.jdbc.support;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.enu.QuartzResultType;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.common.exception.TspTransactionException;
import com.ffcs.crmd.tsp.common.server.ScheduleResult;
import com.ffcs.crmd.tsp.server.core.QuartzSchedulerHandler;
import com.ffcs.crmd.tsp.server.jdbc.DBConnectionManager;
import com.ffcs.crmd.tsp.server.jdbc.delegate.ServiceDelegate;
import com.ffcs.crmd.tsp.server.jdbc.entity.Job;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobDetail;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobDetailParam;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobGroup;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobIdentityInfo;
import com.ffcs.crmd.tsp.server.jdbc.entity.Slice;
import com.ffcs.crmd.tsp.server.jdbc.entity.SliceGroup;
import com.ffcs.crmd.tsp.server.jdbc.entity.TaskIdentity;
import com.ffcs.crmd.tsp.server.manager.JobProcessManager;
import com.ffcs.crmd.tsp.server.manager.TaskRemotingManager;

/**
 * 功能说明:
 *
 * @author FFCS-ZHONGFUHUA
 * 
 * @since 
 *
 */
public abstract class ServiceSupportAbstract implements ServiceSupport {
    /**
     * 日志
     */
    private static final Logger LOG = LoggerFactory.getLogger(LoggerName.ServerLoggerName);
    
    /**
     * 功能说明:
     * 
     * @param jobGroup
     * @throws TspException 
     */
    @Override
    public void addJobGroup(final JobGroup jobGroup) throws TspException {
        execute(new TransactionExecute<Void>() {
            @Override
            public Void execute(Connection conn) throws TspTransactionException {
                getDelegate().addJobGroup(conn, jobGroup);
                return null;
            }
        }, null);
    }
    
    @Override
    public void updateJobGroup(final JobGroup jobGroup) throws TspException {
        execute(new TransactionExecute<Void>() {
            @Override
            public Void execute(Connection conn) throws TspTransactionException {
                getDelegate().updateJobGroup(conn, jobGroup);
                return null;
            }
        }, null);
    }
    
    @Override
    public void deleteJobGroup(final JobGroup jobGroup) throws TspException {
        execute(new TransactionExecute<Void>() {
            @Override
            public Void execute(Connection conn) throws TspTransactionException {
                getDelegate().deleteJobGroup(conn, jobGroup);
                return null;
            }
        }, null);
    }
    
    @Override
    public JobGroup getJobGroup(final JobGroup jobGroup) throws TspException {
        return execute(new TransactionExecute<JobGroup>() {
            @Override
            public JobGroup execute(Connection conn) throws TspTransactionException {
                return getDelegate().getJobGroup(conn, jobGroup);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobGroup
     * @return
     * @throws TspException
     */
    @Override
    public List<JobGroup> queryJobGroup(final JobGroup jobGroup) throws TspException {
        return execute(new TransactionExecute<List<JobGroup>>() {
            @Override
            public List<JobGroup> execute(Connection conn) throws TspTransactionException {
                return getDelegate().queryJobGroup(conn, jobGroup);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobGroup
     * @return
     * @throws TspException
     */
    @Override
    public PageInfo<JobGroup> queryJobGroupPage(final JobGroup jobGroup) throws TspException {
        return execute(new TransactionExecute<PageInfo<JobGroup>>() {
            @Override
            public PageInfo<JobGroup> execute(Connection conn) throws TspTransactionException {
                int pageNumber = jobGroup.getPageNumber();
                int pageSize = jobGroup.getPageSize();
                int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
                jobGroup.setPageNumber(first);
                
                List<JobGroup> jobGroupList = getDelegate().queryJobGroupPage(conn, jobGroup);
                int totalCounts = getDelegate().queryJobGroupSize(conn, jobGroup);
                
                PageInfo<JobGroup> pageInfo = new PageInfo<JobGroup>(jobGroupList);
                pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, jobGroupList);
                return pageInfo;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param job
     * @throws TspException
     */
    @Override
    public void addJob(final Job job) throws TspException {
        execute(new TransactionExecute<Job>() {
            @Override
            public Job execute(Connection conn) throws TspTransactionException {
                getDelegate().addJob(conn, job);
                getJobProcessManager().put(job.getName());
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param job
     * @throws TspException
     */
    @Override
    public void updateJob(final Job job) throws TspException {
        execute(new TransactionExecute<Job>() {
            @Override
            public Job execute(Connection conn) throws TspTransactionException {
                Job jobParam = new Job();
                jobParam.setId(job.getId());
                Job rjob = getDelegate().getJob(conn, job);
                
                if (jobInfoChange(job, rjob)) {
                    JobDetail jobDetailParam = new JobDetail();
                    jobDetailParam.setJobId(job.getId());
                    List<JobDetail> jobDetailList = getDelegate().queryJobDetail(conn, jobDetailParam);
                    for(JobDetail jobDetail : jobDetailList){
                        JobDetailParam jobDetailParamParam = new JobDetailParam();
                        jobDetailParamParam.setJobDetailId(jobDetail.getId());
                        List<JobDetailParam> jobDetailParamList = getDelegate().queryJobDetailParam(conn, jobDetailParamParam);
                        jobDetail.setParams(jobDetailParamList);
                        ScheduleResult pauseScheduleResult = getQuartzSchedulerHandler().pauseJobDetail(jobDetail);
                        if (pauseScheduleResult.getResultType() == QuartzResultType.FAIL) {
                            throw new TspTransactionException(pauseScheduleResult.getErrorMessage());
                        }
                        getJobProcessManager().deleteJobDetail(jobDetail.getJobName(), jobDetail.getName());
                    }
                    
                    ScheduleResult scheduleResult = getQuartzSchedulerHandler().updateJobDetail(jobDetailList, job);
                    if (scheduleResult.getResultType() == QuartzResultType.FAIL) {
                        throw new TspTransactionException(scheduleResult.getErrorMessage());
                    }
                    
                    for(JobDetail jobDetail : jobDetailList){
                        if(jobDetail.getRunning() == 1){
                            ScheduleResult resumeScheduleResult = getQuartzSchedulerHandler().resumeJobDetail(jobDetail);
                            if (resumeScheduleResult.getResultType() == QuartzResultType.FAIL) {
                                throw new TspTransactionException(resumeScheduleResult.getErrorMessage());
                            }
                        }
                    }
                }
                getDelegate().updateJob(conn, job);
                return null;
            }
        }, new TransactionValidator() {
            @Override
            public Boolean validate(Connection conn) throws TspException {
                try {
                    Job jobParam = new Job();
                    jobParam.setId(job.getId());
                    Job rjob = getDelegate().getJob(conn, job);
                    
                    if (jobInfoChange(job, rjob)) {
                        JobDetail jobDetailParam = new JobDetail();
                        jobDetailParam.setJobId(job.getId());
                        List<JobDetail> jobDetailList = getDelegate().queryJobDetail(conn, jobDetailParam);
                        ScheduleResult scheduleResult = getQuartzSchedulerHandler().updateJobDetail(jobDetailList, job);
                        if (scheduleResult.getResultType() == QuartzResultType.FAIL) {
                            throw new TspTransactionException(scheduleResult.getErrorMessage());
                        }
                    }
                } catch (TspTransactionException e) {
                    LOG.error("SERVER-DATABASE: transaction validate exception:", e);
                    throw new TspException("暂停任务失败，提交事务且回滚异常，请联系负责人员进行恢复数据.", e);
                }
                return false;
            }
        });
    }
    
    /**
     * 功能说明:
     * 
     * @param job
     * @throws TspException
     */
    @Override
    public void deleteJob(final Job job) throws TspException {
        execute(new TransactionExecute<Job>() {
            @Override
            public Job execute(Connection conn) throws TspTransactionException {
                try {
                    Job rjob = getJob(job);
                    if(rjob == null){
                        return null;
                    }
                    
                    // 查询任务底下的子任务
                    JobDetail jobDetailParam = new JobDetail();
                    jobDetailParam.setJobId(job.getId());
                    List<JobDetail> jobDetailList = getDelegate().queryJobDetail(conn, jobDetailParam);
                    
                    // 删除job数据库数据
                    getDelegate().deleteJob(conn, job);
                    
                    // 删除quartz里面的子任务
                    ScheduleResult scheduleResult = getQuartzSchedulerHandler().deleteJobDetail(jobDetailList);
                    if (scheduleResult.getResultType() == QuartzResultType.FAIL) {
                        throw new TspTransactionException(scheduleResult.getResultMessage());
                    }
                    
                    // 删除内存数据
                    getJobProcessManager().deleteJob(rjob.getName());
                } catch (TspException e) {
                    throw new TspTransactionException(e);
                }
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param job
     * @return
     * @throws TspException
     */
    @Override
    public Job getJob(final Job job) throws TspException {
        return execute(new TransactionExecute<Job>() {
            @Override
            public Job execute(Connection conn) throws TspTransactionException {
                return getDelegate().getJob(conn, job);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param job
     * @return
     * @throws TspException
     */
    @Override
    public List<Job> queryJob(final Job job) throws TspException {
        return execute(new TransactionExecute<List<Job>>() {
            @Override
            public List<Job> execute(Connection conn) throws TspTransactionException {
                return getDelegate().queryJob(conn, job);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param job
     * @return
     * @throws TspException
     */
    @Override
    public PageInfo<Job> queryJobPage(final Job job) throws TspException {
        return execute(new TransactionExecute<PageInfo<Job>>() {
            @Override
            public PageInfo<Job> execute(Connection conn) throws TspTransactionException {
                int pageNumber = job.getPageNumber();
                int pageSize = job.getPageSize();
                int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
                job.setPageNumber(first);
                
                List<Job> jobDetailList = getDelegate().queryJobPage(conn, job);
                int totalCounts = getDelegate().queryJobSize(conn, job);
                
                PageInfo<Job> pageInfo = new PageInfo<Job>(jobDetailList);
                pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, jobDetailList);
                return pageInfo;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetail
     * @throws TspException
     */
    @Override
    public void addJobDetail(final JobDetail jobDetail) throws TspException {
        execute(new TransactionExecute<JobDetail>() {
            @Override
            public JobDetail execute(Connection conn) throws TspTransactionException {
                Job jobParam = new Job();
                jobParam.setId(jobDetail.getJobId());
                Job job = getDelegate().getJob(conn, jobParam);
                Long jobDetailId = getDelegate().addJobDetail(conn, jobDetail);
                
                if (CollectionUtils.isNotEmpty(jobDetail.getParams())) {
                    for (JobDetailParam jobDetailParam : jobDetail.getParams()) {
                        jobDetailParam.setJobDetailId(jobDetailId);
                        jobDetailParam.setClusterName(jobDetail.getClusterName());
                        getDelegate().addJobDetailParam(conn, jobDetailParam);
                    }
                }
                
                ScheduleResult scheduleResult = getQuartzSchedulerHandler().addJobDetail(jobDetail, job);
                if (scheduleResult.getResultType() == QuartzResultType.FAIL) {
                    throw new TspTransactionException(scheduleResult.getResultMessage());
                }
                return null;
            }
        }, null);
    }
    
    
    /**
     * 功能说明:
     * 
     * @param jobDetail
     * @throws TspException
     */
    @Override
    public void updateJobDetail(final JobDetail jobDetail) throws TspException {
        execute(new TransactionExecute<JobDetail>() {
            @Override
            public JobDetail execute(Connection conn) throws TspTransactionException {
                Job jobParam = new Job();
                jobParam.setId(jobDetail.getJobId());
                Job job = getDelegate().getJob(conn, jobParam);
                
                JobDetailParam jobDetailParam = new JobDetailParam();
                jobDetailParam.setJobDetailId(jobDetail.getId());
                getDelegate().deleteJobDetailParam(conn, jobDetailParam);
                for (JobDetailParam njobDetailParam : jobDetail.getParams()) {
                    getDelegate().addJobDetailParam(conn, njobDetailParam);
                }
                
                ScheduleResult pauseScheduleResult = getQuartzSchedulerHandler().pauseJobDetail(jobDetail);
                if (pauseScheduleResult.getResultType() == QuartzResultType.FAIL) {
                    throw new TspTransactionException(pauseScheduleResult.getErrorMessage());
                }
                
                
                getDelegate().updateJobDetail(conn, jobDetail);
                ScheduleResult scheduleResult = getQuartzSchedulerHandler().updateJobDetail(jobDetail, job);
                if (scheduleResult.getResultType() == QuartzResultType.FAIL) {
                    throw new TspTransactionException(scheduleResult.getErrorMessage());
                }
                
                
                ScheduleResult resumeScheduleResult = getQuartzSchedulerHandler().resumeJobDetail(jobDetail);
                if (resumeScheduleResult.getResultType() == QuartzResultType.FAIL) {
                    throw new TspTransactionException(resumeScheduleResult.getErrorMessage());
                }
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetail
     * @throws TspException
     */
    @Override
    public void deleteJobDetail(final JobDetail jobDetail) throws TspException {
        execute(new TransactionExecute<JobDetail>() {
            @Override
            public JobDetail execute(Connection conn) throws TspTransactionException {
                try {
                    List<JobDetail> jobDetailList = new ArrayList<JobDetail>();
                    JobDetail rjobDetail = getJobDetail(jobDetail);
                    if(rjobDetail != null){
                        jobDetailList.add(rjobDetail);
                    }else{
                        List<JobDetail> rjobDetailList = queryJobDetail(jobDetail);
                        if(CollectionUtils.isNotEmpty(rjobDetailList)){
                            jobDetailList.addAll(rjobDetailList);
                        }
                    }
                    
                    for(JobDetail jobDetail : jobDetailList){
                        // 删除数据库
                        getDelegate().deleteJobDetail(conn, jobDetail);
                        
                        // 删除quartz里面的子任务
                        ScheduleResult scheduleResult = getQuartzSchedulerHandler().deleteJobDetail(jobDetail);
                        if (scheduleResult.getResultType() == QuartzResultType.FAIL) {
                            throw new TspTransactionException(scheduleResult.getResultMessage());
                        }
                        
                        // 删除内存中的子任务
                        getJobProcessManager().deleteJobDetail(jobDetail.getJobName(), jobDetail.getName());
                    }
                } catch (TspException e) {
                    throw new TspTransactionException(e);
                }
                return null;
            }
        }, null);
    }
    
    
    /**
     * 功能说明:
     * 
     * @param jobDetail
     * @throws TspException
     */
    @Override
    public void pauseJobDetail(final JobDetail jobDetail) throws TspException {
        execute(new TransactionExecute<JobDetail>() {
            @Override
            public JobDetail execute(Connection conn) throws TspTransactionException {
                ScheduleResult scheduleResult = getQuartzSchedulerHandler().pauseJobDetail(jobDetail);
                if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                    getDelegate().pauseJobDetail(conn, jobDetail);
                } else {
                    throw new TspTransactionException(scheduleResult.getErrorMessage());
                }
                return null;
            }
        }, new TransactionValidator() {
            @Override
            public Boolean validate(Connection conn) throws TspException {
                ScheduleResult scheduleResult = getQuartzSchedulerHandler().resumeJobDetail(jobDetail);
                if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                    return false;
                } else {
                    throw new TspException(scheduleResult.getErrorMessage());
                }
            }
        });
        
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetail
     * @throws TspException
     */
    @Override
    public void resumeJobDetail(final JobDetail jobDetail) throws TspException {
        execute(new TransactionExecute<JobDetail>() {
            @Override
            public JobDetail execute(Connection conn) throws TspTransactionException {
                ScheduleResult scheduleResult = getQuartzSchedulerHandler().resumeJobDetail(jobDetail);
                if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                    getDelegate().resumeJobDetail(conn, jobDetail);
                } else {
                    throw new TspTransactionException(scheduleResult.getErrorMessage());
                }
                return null;
            }
        }, new TransactionValidator() {
            @Override
            public Boolean validate(Connection conn) throws TspException {
                ScheduleResult scheduleResult = getQuartzSchedulerHandler().pauseJobDetail(jobDetail);
                if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                    return false;
                } else {
                    throw new TspException(scheduleResult.getErrorMessage());
                }
            }
        });
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetail
     * @return
     * @throws TspException
     */
    @Override
    public JobDetail getJobDetail(final JobDetail jobDetail) throws TspException {
        return execute(new TransactionExecute<JobDetail>() {
            @Override
            public JobDetail execute(Connection conn) throws TspTransactionException {
                return getDelegate().getJobDetail(conn, jobDetail);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetail
     * @return
     * @throws TspException
     */
    @Override
    public List<JobDetail> queryJobDetail() throws TspException {
        return execute(new TransactionExecute<List<JobDetail>>() {
            @Override
            public List<JobDetail> execute(Connection conn) throws TspTransactionException {
                return getDelegate().queryJobDetail(conn, new JobDetail());
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetail
     * @return
     * @throws TspException
     */
    @Override
    public List<JobDetail> queryJobDetail(final JobDetail jobDetail) throws TspException {
        return execute(new TransactionExecute<List<JobDetail>>() {
            @Override
            public List<JobDetail> execute(Connection conn) throws TspTransactionException {
                return getDelegate().queryJobDetail(conn, jobDetail);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetail
     * @return
     * @throws TspException
     */
    @Override
    public PageInfo<JobDetail> queryJobDetailPage(final JobDetail jobDetail) throws TspException {
        return execute(new TransactionExecute<PageInfo<JobDetail>>() {
            @Override
            public PageInfo<JobDetail> execute(Connection conn) throws TspTransactionException {
                int pageNumber = jobDetail.getPageNumber();
                int pageSize = jobDetail.getPageSize();
                int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
                jobDetail.setPageNumber(first);
                
                List<JobDetail> jobDetailList = getDelegate().queryJobDetailPage(conn, jobDetail);
                int totalCounts = getDelegate().queryJobDetailSize(conn, jobDetail);
                
                for (JobDetail rjobDetail : jobDetailList) {
                    JobDetail jobDetail = getQuartzSchedulerHandler().getJobDetail(rjobDetail.getJobName(), rjobDetail.getName());
                    if(jobDetail != null){
                        rjobDetail.setRuntimes(getJobProcessManager().getRuntimes(rjobDetail.getJobName(), rjobDetail.getName()));
                        rjobDetail.setNextTime(jobDetail.getNextTime());
                        rjobDetail.setPrevTime(jobDetail.getPrevTime());
                        rjobDetail.setStartTime(jobDetail.getStartTime());
                        rjobDetail.setEndTime(jobDetail.getEndTime());
                        rjobDetail.setRunning(jobDetail.getRunning());
                    }
                }
                
                PageInfo<JobDetail> pageInfo = new PageInfo<JobDetail>(jobDetailList);
                pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, jobDetailList);
                return pageInfo;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetailParam
     * @throws TspException
     */
    @Override
    public void addJobDetailParam(final JobDetailParam jobDetailParam) throws TspException {
        execute(new TransactionExecute<JobDetailParam>() {
            @Override
            public JobDetailParam execute(Connection conn) throws TspTransactionException {
                getDelegate().addJobDetailParam(conn, jobDetailParam);
                updateJobDetail(conn, jobDetailParam.getJobDetailId());
                return null;
            }
        }, null);
    }
    
    
    private void updateJobDetail(Connection conn, Long jobDetailId) throws TspTransactionException{
        JobDetailParam jobDetailParamParam = new JobDetailParam();
        jobDetailParamParam.setJobDetailId(jobDetailId);
        List<JobDetailParam> jobDetailParamList = getDelegate().queryJobDetailParam(conn, jobDetailParamParam);
        
        JobDetail jobDetailParam = new JobDetail();
        jobDetailParam.setId(jobDetailParamParam.getJobDetailId());
        JobDetail jobDetail = getDelegate().getJobDetail(conn, jobDetailParam);
        jobDetail.setParams(jobDetailParamList);
        
        Job jobParam = new Job();
        jobParam.setId(jobDetail.getJobId());
        Job job = getDelegate().getJob(conn, jobParam);
        
        ScheduleResult pauseScheduleResult = getQuartzSchedulerHandler().pauseJobDetail(jobDetail);
        if (pauseScheduleResult.getResultType() == QuartzResultType.FAIL) {
            throw new TspTransactionException(pauseScheduleResult.getErrorMessage());
        }
        getJobProcessManager().deleteJobDetail(jobDetail.getJobName(), jobDetail.getName());
        
        ScheduleResult scheduleResult = getQuartzSchedulerHandler().updateJobDetail(jobDetail, job);
        if (scheduleResult.getResultType() == QuartzResultType.FAIL) {
            throw new TspTransactionException(scheduleResult.getErrorMessage());
        }
        
        if(jobDetail.getRunning() == 1){
            ScheduleResult resumeScheduleResult = getQuartzSchedulerHandler().resumeJobDetail(jobDetail);
            if (resumeScheduleResult.getResultType() == QuartzResultType.FAIL) {
                throw new TspTransactionException(resumeScheduleResult.getErrorMessage());
            }
        }
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetailParam
     * @throws TspException
     */
    @Override
    public void updateJobDetailParam(final JobDetailParam jobDetailParam) throws TspException {
        execute(new TransactionExecute<JobDetailParam>() {
            @Override
            public JobDetailParam execute(Connection conn) throws TspTransactionException {
                getDelegate().updateJobDetailParam(conn, jobDetailParam);
                updateJobDetail(conn, jobDetailParam.getJobDetailId());
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetailParam
     * @throws TspException
     */
    @Override
    public void deleteJobDetailParam(final JobDetailParam jobDetailParam) throws TspException {
        execute(new TransactionExecute<JobDetailParam>() {
            @Override
            public JobDetailParam execute(Connection conn) throws TspTransactionException {
                try {
                    JobDetailParam rjobDetailParam = getJobDetailParam(jobDetailParam);
                    if(rjobDetailParam == null){
                        return null;
                    }
                    getDelegate().deleteJobDetailParam(conn, jobDetailParam);
                    updateJobDetail(conn, rjobDetailParam.getJobDetailId());
                } catch (TspException e) {
                   throw new TspTransactionException(e);
                }
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetailParam
     * @return
     * @throws TspException
     */
    @Override
    public JobDetailParam getJobDetailParam(final JobDetailParam jobDetailParam) throws TspException {
        return execute(new TransactionExecute<JobDetailParam>() {
            @Override
            public JobDetailParam execute(Connection conn) throws TspTransactionException {
                return getDelegate().getJobDetailParam(conn, jobDetailParam);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetailParam
     * @return
     * @throws TspException
     */
    @Override
    public List<JobDetailParam> queryJobDetailParam(final JobDetailParam jobDetailParam) throws TspException {
        return execute(new TransactionExecute<List<JobDetailParam>>() {
            @Override
            public List<JobDetailParam> execute(Connection conn) throws TspTransactionException {
                return getDelegate().queryJobDetailParam(conn, jobDetailParam);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobDetailParam
     * @return
     * @throws TspException
     */
    @Override
    public PageInfo<JobDetailParam> queryJobDetailParamPage(final JobDetailParam jobDetailParam) throws TspException {
        return execute(new TransactionExecute<PageInfo<JobDetailParam>>() {
            @Override
            public PageInfo<JobDetailParam> execute(Connection conn) throws TspTransactionException {
                int pageNumber = jobDetailParam.getPageNumber();
                int pageSize = jobDetailParam.getPageSize();
                int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
                jobDetailParam.setPageNumber(first);
                
                List<JobDetailParam> jobDetailList = getDelegate().queryJobDetailParamPage(conn, jobDetailParam);
                int totalCounts = getDelegate().queryJobDetailParamSize(conn, jobDetailParam);
                
                PageInfo<JobDetailParam> pageInfo = new PageInfo<JobDetailParam>(jobDetailList);
                pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, jobDetailList);
                return pageInfo;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobIdentityInfo
     * @throws TspException
     */
    @Override
    public void addJobIdentityInfo(final JobIdentityInfo jobIdentityInfo) throws TspException {
        execute(new TransactionExecute<JobIdentityInfo>() {
            @Override
            public JobIdentityInfo execute(Connection conn) throws TspTransactionException {
                Job jobParam = new Job();
                jobParam.setId(jobIdentityInfo.getJobId());
                Job job = getDelegate().getJob(conn, jobParam);
                getTaskRemotingManager().updateJobChannel(job.getName(), jobIdentityInfo.getIdentity());
                getDelegate().addJobIdentityInfo(conn, jobIdentityInfo);
                return null;
            }
        }, null);
    }
    
    
    /**
     * 功能说明:
     * 
     * @param jobIdentityInfo
     * @throws TspException
     */
    @Override
    public void deleteJobIdentityInfo(final JobIdentityInfo jobIdentityInfo) throws TspException {
        execute(new TransactionExecute<JobIdentityInfo>() {
            @Override
            public JobIdentityInfo execute(Connection conn) throws TspTransactionException {
                
                JobIdentityInfo rjobIdentityInfo = getDelegate().getJobIdentityInfo(conn, jobIdentityInfo);
                Job jobParam = new Job();
                jobParam.setId(rjobIdentityInfo.getJobId());
                Job job = getDelegate().getJob(conn, jobParam);
                
                getTaskRemotingManager().deleteJobChannel(job.getName(), rjobIdentityInfo.getIdentity());
                getDelegate().deleteJobIdentityInfo(conn, jobIdentityInfo);
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobIdentityInfo
     * @return
     * @throws TspException
     */
    @Override
    public JobIdentityInfo getJobIdentityInfo(final JobIdentityInfo jobIdentityInfo) throws TspException {
        return execute(new TransactionExecute<JobIdentityInfo>() {
            @Override
            public JobIdentityInfo execute(Connection conn) throws TspTransactionException {
                return getDelegate().getJobIdentityInfo(conn, jobIdentityInfo);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobIdentityInfo
     * @return
     * @throws TspException
     */
    @Override
    public List<JobIdentityInfo> queryJobIdentityInfo(final JobIdentityInfo jobIdentityInfo) throws TspException {
        return execute(new TransactionExecute<List<JobIdentityInfo>>() {
            @Override
            public List<JobIdentityInfo> execute(Connection conn) throws TspTransactionException {
                return getDelegate().queryJobIdentityInfo(conn, jobIdentityInfo);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param jobIdentityInfo
     * @return
     * @throws TspException
     */
    @Override
    public PageInfo<JobIdentityInfo> queryJobIdentityInfoPage(final JobIdentityInfo jobIdentityInfo) throws TspException {
        return execute(new TransactionExecute<PageInfo<JobIdentityInfo>>() {
            @Override
            public PageInfo<JobIdentityInfo> execute(Connection conn) throws TspTransactionException {
                int pageNumber = jobIdentityInfo.getPageNumber();
                int pageSize = jobIdentityInfo.getPageSize();
                int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
                jobIdentityInfo.setPageNumber(first);
                
                List<JobIdentityInfo> jobGroupList = getDelegate().queryJobIdentityInfoPage(conn, jobIdentityInfo);
                int totalCounts = getDelegate().queryJobIdentityInfoSize(conn, jobIdentityInfo);
                
                PageInfo<JobIdentityInfo> pageInfo = new PageInfo<JobIdentityInfo>(jobGroupList);
                pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, jobGroupList);
                return pageInfo;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param slice
     * @throws TspException
     */
    @Override
    public void addSlice(final Slice slice) throws TspException {
        execute(new TransactionExecute<Slice>() {
            @Override
            public Slice execute(Connection conn) throws TspTransactionException {
                getDelegate().addSlice(conn, slice);
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param slice
     * @throws TspException
     */
    @Override
    public void updateSlice(final Slice slice) throws TspException {
        execute(new TransactionExecute<Slice>() {
            @Override
            public Slice execute(Connection conn) throws TspTransactionException {
                getDelegate().updateSlice(conn, slice);
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param slice
     * @throws TspException
     */
    @Override
    public void deleteSlice(final Slice slice) throws TspException {
        execute(new TransactionExecute<Slice>() {
            @Override
            public Slice execute(Connection conn) throws TspTransactionException {
                getDelegate().deleteSlice(conn, slice);
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param slice
     * @return
     * @throws TspException
     */
    @Override
    public Slice getSlice(final Slice slice) throws TspException {
        return execute(new TransactionExecute<Slice>() {
            @Override
            public Slice execute(Connection conn) throws TspTransactionException {
                return getDelegate().getSlice(conn, slice);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param slice
     * @return
     * @throws TspException
     */
    @Override
    public List<Slice> querySlice(final Slice slice) throws TspException {
        return execute(new TransactionExecute<List<Slice>>() {
            @Override
            public List<Slice> execute(Connection conn) throws TspTransactionException {
                return getDelegate().querySlice(conn, slice);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param slice
     * @return
     * @throws TspException
     */
    @Override
    public PageInfo<Slice> querySlicePage(final Slice slice) throws TspException {
        return execute(new TransactionExecute<PageInfo<Slice>>() {
            @Override
            public PageInfo<Slice> execute(Connection conn) throws TspTransactionException {
                int pageNumber = slice.getPageNumber();
                int pageSize = slice.getPageSize();
                int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
                slice.setPageNumber(first);
                
                List<Slice> jobDetailList = getDelegate().querySlicePage(conn, slice);
                int totalCounts = getDelegate().querySliceSize(conn, slice);
                
                PageInfo<Slice> pageInfo = new PageInfo<Slice>(jobDetailList);
                pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, jobDetailList);
                return pageInfo;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param sliceGroup
     * @throws TspException
     */
    @Override
    public void addSliceGroup(final SliceGroup sliceGroup) throws TspException {
        execute(new TransactionExecute<SliceGroup>() {
            @Override
            public SliceGroup execute(Connection conn) throws TspTransactionException {
                getDelegate().addSliceGroup(conn, sliceGroup);
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param sliceGroup
     * @throws TspException
     */
    @Override
    public void updateSliceGroup(final SliceGroup sliceGroup) throws TspException {
        execute(new TransactionExecute<SliceGroup>() {
            @Override
            public SliceGroup execute(Connection conn) throws TspTransactionException {
                getDelegate().updateSliceGroup(conn, sliceGroup);
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param sliceGroup
     * @throws TspException
     */
    @Override
    public void deleteSliceGroup(final SliceGroup sliceGroup) throws TspException {
        execute(new TransactionExecute<SliceGroup>() {
            @Override
            public SliceGroup execute(Connection conn) throws TspTransactionException {
                getDelegate().deleteSliceGroup(conn, sliceGroup);
                return null;
            }
        }, null);
    }
    
    
    /**
     * 功能说明:
     * 
     * @param sliceGroup
     * @return
     * @throws TspException
     */
    @Override
    public SliceGroup getSliceGroup(final SliceGroup sliceGroup) throws TspException {
        return execute(new TransactionExecute<SliceGroup>() {
            @Override
            public SliceGroup execute(Connection conn) throws TspTransactionException {
                return getDelegate().getSliceGroup(conn, sliceGroup);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param sliceGroup
     * @return
     * @throws TspException
     */
    @Override
    public List<SliceGroup> querySliceGroup(final SliceGroup sliceGroup) throws TspException {
        return execute(new TransactionExecute<List<SliceGroup>>() {
            @Override
            public List<SliceGroup> execute(Connection conn) throws TspTransactionException {
                return getDelegate().querySliceGroup(conn, sliceGroup);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param sliceGroup
     * @return
     * @throws TspException
     */
    @Override
    public PageInfo<SliceGroup> querySliceGroupPage(final SliceGroup sliceGroup) throws TspException {
        return execute(new TransactionExecute<PageInfo<SliceGroup>>() {
            @Override
            public PageInfo<SliceGroup> execute(Connection conn) throws TspTransactionException {
                int pageNumber = sliceGroup.getPageNumber();
                int pageSize = sliceGroup.getPageSize();
                int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
                sliceGroup.setPageNumber(first);
                
                List<SliceGroup> jobDetailList = getDelegate().querySliceGroupPage(conn, sliceGroup);
                int totalCounts = getDelegate().querySliceGroupSize(conn, sliceGroup);
                
                PageInfo<SliceGroup> pageInfo = new PageInfo<SliceGroup>(jobDetailList);
                pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, jobDetailList);
                return pageInfo;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param taskIdentity
     * @throws TspException
     */
    @Override
    public void addTaskIdentity(final TaskIdentity taskIdentity) throws TspException {
        execute(new TransactionExecute<TaskIdentity>() {
            @Override
            public TaskIdentity execute(Connection conn) throws TspTransactionException {
                getDelegate().addTaskIdentity(conn, taskIdentity);
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param taskIdentity
     * @throws TspException
     */
    @Override
    public void updateTaskIdentity(final TaskIdentity taskIdentity) throws TspException {
        execute(new TransactionExecute<TaskIdentity>() {
            @Override
            public TaskIdentity execute(Connection conn) throws TspTransactionException {
                getDelegate().updateTaskIdentity(conn, taskIdentity);
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param taskIdentity
     * @throws TspException
     */
    @Override
    public void deleteTaskIdentity(final TaskIdentity taskIdentity) throws TspException {
        execute(new TransactionExecute<TaskIdentity>() {
            @Override
            public TaskIdentity execute(Connection conn) throws TspTransactionException {
                getDelegate().deleteTaskIdentity(conn, taskIdentity);
                return null;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param taskIdentity
     * @return
     * @throws TspException
     */
    @Override
    public TaskIdentity getTaskIdentity(final TaskIdentity taskIdentity) throws TspException {
        return execute(new TransactionExecute<TaskIdentity>() {
            @Override
            public TaskIdentity execute(Connection conn) throws TspTransactionException {
                return getDelegate().getTaskIdentity(conn, taskIdentity);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param taskIdentity
     * @return
     * @throws TspException
     */
    @Override
    public List<TaskIdentity> queryTaskIdentity(final TaskIdentity taskIdentity) throws TspException {
        return execute(new TransactionExecute<List<TaskIdentity>>() {
            @Override
            public List<TaskIdentity> execute(Connection conn) throws TspTransactionException {
                return getDelegate().queryTaskIdentity(conn, taskIdentity);
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param taskIdentity
     * @return
     * @throws TspException
     */
    @Override
    public PageInfo<TaskIdentity> queryTaskIdentityPage(final TaskIdentity taskIdentity) throws TspException {
        return execute(new TransactionExecute<PageInfo<TaskIdentity>>() {
            @Override
            public PageInfo<TaskIdentity> execute(Connection conn) throws TspTransactionException {
                int pageNumber = taskIdentity.getPageNumber();
                int pageSize = taskIdentity.getPageSize();
                int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
                taskIdentity.setPageNumber(first);
                
                List<TaskIdentity> jobDetailList = getDelegate().queryTaskIdentityPage(conn, taskIdentity);
                int totalCounts = getDelegate().queryTaskIdentitySize(conn, taskIdentity);
                
                PageInfo<TaskIdentity> pageInfo = new PageInfo<TaskIdentity>(jobDetailList);
                pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, jobDetailList);
                return pageInfo;
            }
        }, null);
    }
    
    /**
     * 功能说明:
     * 
     * @param job
     * @throws TspException
     */
    @Override
    public void pauseJob(final Job job) throws TspException {
        execute(new TransactionExecute<Job>() {
            @Override
            public Job execute(Connection conn) throws TspTransactionException {
                Job rjob = getDelegate().getJob(conn, job);
                ScheduleResult scheduleResult = getQuartzSchedulerHandler().pauseJob(rjob);
                if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                    JobDetail jobDetailParam = new JobDetail();
                    jobDetailParam.setJobId(rjob.getId());
                    getDelegate().pauseJobDetail(conn, jobDetailParam);
                } else {
                    throw new TspTransactionException(scheduleResult.getErrorMessage());
                }
                return null;
            }
        }, new TransactionValidator() {
            @Override
            public Boolean validate(Connection conn) throws TspException {
                try {
                    Job rjob = getDelegate().getJob(conn, job);
                    ScheduleResult scheduleResult = getQuartzSchedulerHandler().resumeJob(rjob);
                    if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                        return false;
                    } else {
                        throw new TspException(scheduleResult.getErrorMessage());
                    }
                } catch (TspTransactionException e) {
                    LOG.error("SERVER-DATABASE: transaction validate exception:", e);
                    throw new TspException("暂停任务失败，提交事务且回滚异常，请联系负责人员进行恢复数据.", e);
                }
            }
        });
    }
    
    /**
     * 功能说明:
     * 
     * @param jobGroup
     * @throws TspException
     */
    @Override
    public void pauseJobGroup(final JobGroup jobGroup) throws TspException {
        execute(new TransactionExecute<JobGroup>() {
            @Override
            public JobGroup execute(Connection conn) throws TspTransactionException {
                JobGroup rjobGroup = getDelegate().getJobGroup(conn, jobGroup);
                
                Job jobParam = new Job();
                jobParam.setJobGroupId(rjobGroup.getId());
                List<Job> jobList = getDelegate().queryJob(conn, jobParam);
                
                for (Job job : jobList) {
                    getQuartzSchedulerHandler().pauseJob(job);
                    
                    JobDetail jobDetailParam = new JobDetail();
                    jobDetailParam.setJobId(job.getId());
                    getDelegate().pauseJobDetail(conn, jobDetailParam);
                }
                return null;
            }
        }, new TransactionValidator() {
            @Override
            public Boolean validate(Connection conn) throws TspException {
                try {
                    JobGroup rjobGroup = getDelegate().getJobGroup(conn, jobGroup);
                    
                    Job jobParam = new Job();
                    jobParam.setJobGroupId(rjobGroup.getId());
                    List<Job> jobList = getDelegate().queryJob(conn, jobParam);
                    
                    for (Job job : jobList) {
                        ScheduleResult scheduleResult = getQuartzSchedulerHandler().resumeJob(job);
                        if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                            return false;
                        } else {
                            throw new TspException(scheduleResult.getErrorMessage());
                        }
                    }
                } catch (TspTransactionException e) {
                    LOG.error("SERVER-DATABASE: transaction validate exception:", e);
                    throw new TspException("暂停任务失败，提交事务且回滚异常，请联系负责人员进行恢复数据.", e);
                }
                return true;
            }
        });
    }
    
    /**
     * 功能说明:
     * 
     * @param job
     * @throws TspException
     */
    @Override
    public void resumeJob(final Job job) throws TspException {
        execute(new TransactionExecute<Job>() {
            @Override
            public Job execute(Connection conn) throws TspTransactionException {
                Job rjob = getDelegate().getJob(conn, job);
                ScheduleResult scheduleResult = getQuartzSchedulerHandler().resumeJob(rjob);
                if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                    JobDetail jobDetailParam = new JobDetail();
                    jobDetailParam.setJobId(rjob.getId());
                    getDelegate().resumeJobDetail(conn, jobDetailParam);
                } else {
                    throw new TspTransactionException(scheduleResult.getErrorMessage());
                }
                return null;
            }
        }, new TransactionValidator() {
            @Override
            public Boolean validate(Connection conn) throws TspException {
                try {
                    Job rjob = getDelegate().getJob(conn, job);
                    ScheduleResult scheduleResult = getQuartzSchedulerHandler().pauseJob(rjob);
                    if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                        return false;
                    } else {
                        throw new TspException(scheduleResult.getErrorMessage());
                    }
                } catch (TspTransactionException e) {
                    LOG.error("SERVER-DATABASE: transaction validate exception:", e);
                    throw new TspException("暂停任务失败，提交事务且回滚异常，请联系负责人员进行恢复数据.", e);
                }
            }
        });
    }
    
    /**
     * 功能说明:
     * 
     * @param jobGroup
     * @throws TspException
     */
    @Override
    public void resumeJobGroup(final JobGroup jobGroup) throws TspException {
        execute(new TransactionExecute<JobGroup>() {
            @Override
            public JobGroup execute(Connection conn) throws TspTransactionException {
                JobGroup rjobGroup = getDelegate().getJobGroup(conn, jobGroup);
                
                Job jobParam = new Job();
                jobParam.setJobGroupId(rjobGroup.getId());
                List<Job> jobList = getDelegate().queryJob(conn, jobParam);
                
                for (Job job : jobList) {
                    getQuartzSchedulerHandler().resumeJob(job);
                    JobDetail jobDetailParam = new JobDetail();
                    jobDetailParam.setJobId(job.getId());
                    getDelegate().resumeJobDetail(conn, jobDetailParam);
                }
                return null;
            }
        }, new TransactionValidator() {
            @Override
            public Boolean validate(Connection conn) throws TspException {
                try {
                    JobGroup rjobGroup = getDelegate().getJobGroup(conn, jobGroup);
                    
                    Job jobParam = new Job();
                    jobParam.setJobGroupId(rjobGroup.getId());
                    List<Job> jobList = getDelegate().queryJob(conn, jobParam);
                    
                    for (Job job : jobList) {
                        ScheduleResult scheduleResult = getQuartzSchedulerHandler().pauseJob(job);
                        if (scheduleResult.getResultType() == QuartzResultType.SUCCESS) {
                            return false;
                        } else {
                            throw new TspException(scheduleResult.getErrorMessage());
                        }
                    }
                } catch (TspTransactionException e) {
                    LOG.error("SERVER-DATABASE: transaction validate exception:", e);
                    throw new TspException("暂停任务失败，提交事务且回滚异常，请联系负责人员进行恢复数据.", e);
                }
                return true;
            }
        });
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param txExecute
     * @param txValidator
     * @return
     * @throws TspException
     */
    protected <T> T execute(TransactionExecute<T> txExecute, final TransactionValidator txValidator) throws TspException {
        Connection conn = null;
        try {
            conn = getConnection();
            final T result = txExecute.execute(conn);
            commitConnection(conn);
            return result;
        } catch (TspTransactionException e) {
            if (txValidator != null) {
                boolean validate = txValidator.validate(conn);
                if (!validate) {
                    rollbackConnection(conn);
                }
            } else {
                rollbackConnection(conn);
            }
            throw new TspException(e);
        } finally {
            closeConnection(conn);
        }
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @return
     * @throws TspException
     */
    protected Connection getConnection() throws TspException {
        Connection conn = null;
        try {
            conn = DBConnectionManager.getInstance().getConnection();
        } catch (SQLException e) {
            LOG.warn("SERVER-DATABASE: Get a connection exception:", e);
        }
        
        if (conn == null) {
            throw new TspException("Could not get connection from DataSource");
        }
        
        try {
            if (!isAutoCommit()) {
                conn.setAutoCommit(false);
            }
            
            if (isTxIsolationLevelSerializable()) {
                conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
            }
        } catch (SQLException e) {
            LOG.warn("SERVER-DATABASE: Failed to override connection auto commit/transaction isolation:", e);
            closeConnection(conn);
            throw new TspException(e);
        }
        return conn;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param conn
     * @throws TspException
     */
    protected void commitConnection(Connection conn) throws TspTransactionException {
        if (conn != null) {
            try {
                conn.commit();
            } catch (SQLException e) {
                throw new TspTransactionException("Couldn't commit jdbc connection: ", e);
            }
        }
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param conn
     */
    protected void rollbackConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException e) {
                LOG.error("SERVER-DATABASE: Couldn't rollback jdbc connection: ", e);
            }
        }
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param conn
     */
    protected void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                LOG.error("SERVER-DATABASE: Couldn't close jdbc connection: ", e);
            }
        }
    }
    
    protected abstract ServiceDelegate getDelegate();
    
    protected abstract QuartzSchedulerHandler getQuartzSchedulerHandler();
    
    protected abstract JobProcessManager getJobProcessManager();
    
    protected abstract TaskRemotingManager getTaskRemotingManager();
    
    protected abstract void setDelegate(ServiceDelegate delegate);
    
    protected abstract boolean isAutoCommit();
    
    protected abstract boolean isTxIsolationLevelSerializable();
    
    protected interface TransactionExecute<T> {
        T execute(Connection conn) throws TspTransactionException;
    }
    
    protected interface TransactionValidator {
        Boolean validate(Connection conn) throws TspException;
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param job
     * @param rjob
     * @return
     */
    private boolean jobInfoChange(Job job, Job rjob) {
        if (!rjob.getJobClass().equals(job.getJobClass())) {
            return true;
        }
        
        if ((rjob.getRepeatType() != null && job.getRepeatType() != null)
            && rjob.getRepeatType().shortValue() != job.getRepeatType().shortValue()) {
            return true;
        }
        
        if ((rjob.getType() != null && job.getType() != null) && rjob.getType().shortValue() != job.getType().shortValue()) {
            return true;
        }
        
        if(rjob.getRepeatType() == 0){
            if ((rjob.getRepeatCount() != null && job.getRepeatCount() != null)
                && rjob.getRepeatCount().intValue() != job.getRepeatCount().intValue()) {
                return true;
            }
            
            if ((rjob.getRepeatInterval() != null && job.getRepeatInterval() != null)
                && rjob.getRepeatInterval().longValue() != job.getRepeatInterval().longValue()) {
                return true;
            }
        }else{
            if ((StringUtils.isNoneBlank(rjob.getCronExpression()) && StringUtils.isNoneBlank(job.getCronExpression()))
                && !rjob.getCronExpression().equals(job.getCronExpression())) {
                return true;
            }
        }
        return false;
    }

}
